<?php

namespace app\api\controller;

class Room
{
    public $pagesize = 10;


    public function lists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        $w['uid'] = $userInfo['id'];
        //获取所有的 设备
        $deviceListTemp = (new \app\admin\model\NewwindDevice())->getDataForWheres(
            [
                'uid' => $userInfo['id'],
                'api_stats' => 0
            ]
        );
        $ids = [];
        if ($deviceListTemp) {
            foreach ($deviceListTemp as $k => $v) {
                $ids[] = $v['id'];
            }
        }
        if ($ids) {
            $url = "http://8.131.73.193:90/demo/device_get_property.php";
            $param = array(
                'ids' => implode(",", $ids),
            );
            get_url($url, $param);
        }
        $model = new \app\admin\model\NewwindRoom();
        $lists = $model->getDataForWheres($w, "*", "flags desc,id asc");

        foreach ($lists as $key => $val) {
            $lists[$key]['devlists'] = array();
            $deviceLists = $this->getDeviceList($val['id']);
            if ($deviceLists) {
                $lists[$key]['devlists'] = $deviceLists;
            }
        }


        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);

    }

    public function getDeviceList($room_id)
    {
        $model = new \app\admin\model\NewwindDeviceView();
        $w['room_id'] = $room_id;
        $lists = $model->getDataForWheres($w, "id,devicename,nickname,devicenickname,devicesecret,room_id,product_id,iot_id,api_stats,device_data,last_get_device_data_time,imgurl", "id desc");
        return $lists;

    }


    public function addData()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        $name = trim(input("request.name"));//auth
        if (empty($name)) {
            $return = ['err' => 1, 'msg' => '房间名字不能为空', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        $data = array(
            'uid' => $userInfo['id'],
            'ctime' => date("Y-m-d H:i:s"),
            'name' => $name,
            'flags' => 0,
        );


        $model = new \app\admin\model\NewwindRoom();
        $res = $model->add($data);
        if ($res) {
            $data['id'] = $res;
            $return = ['err' => 0, 'msg' => '操作成功', 'data' => $data];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }


    public function rename()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        $room_id = trim(input("request.room_id"));//用户auth
        if (empty($room_id)) {
            $return = ['err' => 1, 'msg' => '房间ID不能为空'];
            return json($return);
        }

        $name = trim(input("request.name"));//用户auth
        if (empty($name)) {
            $return = ['err' => 1, 'msg' => '房间名称不能为空'];
            return json($return);
        }
        $model = new \app\admin\model\NewwindRoom();
        $old = $model->getID($room_id);
        if (empty($old)) {
            $return = ['err' => 1, 'msg' => '房间不存在'];
            return json($return);
        }

        $res = $model->updateDataForWhere(array('name' => $name), array('id' => $room_id));

        if ($res || ($res === 0)) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    public function delData()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        $room_id = trim(input("request.room_id"));//用户auth
        if (empty($room_id)) {
            $return = ['err' => 1, 'msg' => '房间ID不能为空'];
            return json($return);
        }

        $deviceLists = $this->getDeviceList($room_id);
        if (!empty($deviceLists)) {
            $return = ['err' => 1, 'msg' => '房间里还有设备，不能删除'];
            return json($return);
        }
        $model = new \app\admin\model\NewwindRoom();
        $old = $model->getID($room_id);
        if (empty($old)) {
            $return = ['err' => 1, 'msg' => '房间不存在'];
            return json($return);
        }
        if ($old['flags'] == 1) {
            $return = ['err' => 1, 'msg' => '默认房间不能删除'];
            return json($return);
        }
        $res = $model->delData(
            array('uid' => $userInfo['id'], 'id' => $room_id)
        );
        if ($res || ($res === 0)) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    public function addDevice()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        $room_id = trim(input("request.room_id"));//用户auth
        if (empty($room_id)) {
            $return = ['err' => 1, 'msg' => '房间ID不能为空'];
            return json($return);
        }

        $roomInfo = (new \app\admin\model\NewwindRoom())->getDataForWhere(['id' => $room_id, 'uid' => $userInfo['id']]);
        if (empty($roomInfo)) {
            $return = ['err' => 1, 'msg' => '房间ID不正确'];
            return json($return);
        }
        $devicename = trim(input("request.devicename"));//用户auth
        if (empty($devicename)) {
            $return = ['err' => 1, 'msg' => '设备标识不能为空'];
            return json($return);
        }
        $model = new \app\admin\model\NewwindDevice();
        $deviceInfo = $model->getDataForWhere(['devicename' => $devicename]);
        if (empty($deviceInfo)) {
            $return = ['err' => 1, 'msg' => '设备不存在'];
            return json($return);
        }
        if (!empty($deviceInfo['uid'])) {
            if ($deviceInfo['uid'] != $userInfo['id']) {
                $return = ['err' => 100, 'msg' => '该设备已经被其他用户绑定'];
                return json($return);
            }
            $res = $model->updateDataForWhere(['uid' => $userInfo['id'], 'room_id' => $room_id], ['id' => $deviceInfo['id']]);
            if ($res || $res === 0) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }

        } else {
            $res = $model->updateDataForWhere(['uid' => $userInfo['id'], 'room_id' => $room_id], ['id' => $deviceInfo['id']]);
            if ($res || $res === 0) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


    }




    public function updateDeviceName()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        $devicenickname = trim(input("request.devicenickname"));//用户auth
        if (empty($devicenickname)) {
            $return = ['err' => 1, 'msg' => '设备名称不能为空'];
            return json($return);
        }


        $devicename = trim(input("request.devicename"));//用户auth
        if (empty($devicename)) {
            $return = ['err' => 1, 'msg' => '设备标识不能为空'];
            return json($return);
        }
        $model = new \app\admin\model\NewwindDevice();
        $deviceInfo = $model->getDataForWhere(['devicename' => $devicename]);
        if (empty($deviceInfo)) {
            $return = ['err' => 1, 'msg' => '设备不存在'];
            return json($return);
        }

        if (!empty($deviceInfo['uid'])) {
            if ($deviceInfo['uid'] != $userInfo['id']) {
                $return = ['err' => 100, 'msg' => '你不能操作该设备'];
                return json($return);
            }
            $res = $model->updateDataForWhere(['uid' => $userInfo['id'], 'devicenickname' => $devicenickname], ['id' => $deviceInfo['id']]);
            if ($res || $res === 0) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }

        } else {
            $res = $model->updateDataForWhere(['uid' => $userInfo['id'], 'devicenickname' => $devicenickname], ['id' => $deviceInfo['id']]);
            if ($res || $res === 0) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


    }



    public function changeRoomForDevice()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        $room_id = trim(input("request.room_id"));//用户auth
        if (empty($room_id)) {
            $return = ['err' => 1, 'msg' => '房间ID不能为空'];
            return json($return);
        }

        $roomInfo = (new \app\admin\model\NewwindRoom())->getDataForWhere(['id' => $room_id, 'uid' => $userInfo['id']]);
        if (empty($roomInfo)) {
            $return = ['err' => 1, 'msg' => '房间ID不正确'];
            return json($return);
        }
        $devicename = trim(input("request.devicename"));//用户auth
        if (empty($devicename)) {
            $return = ['err' => 1, 'msg' => '设备标识不能为空'];
            return json($return);
        }
        $model = new \app\admin\model\NewwindDevice();
        $deviceInfo = $model->getDataForWhere(['devicename' => $devicename]);
        if (empty($deviceInfo)) {
            $return = ['err' => 1, 'msg' => '设备不存在'];
            return json($return);
        }
        if (!empty($deviceInfo['uid'])) {
            if ($deviceInfo['uid'] != $userInfo['id']) {
                $return = ['err' => 100, 'msg' => '该设备已经被其他用户绑定'];
                return json($return);
            }
            $res = $model->updateDataForWhere(['uid' => $userInfo['id'], 'room_id' => $room_id], ['id' => $deviceInfo['id']]);
            if ($res || $res === 0) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }

        } else {
            $res = $model->updateDataForWhere(['uid' => $userInfo['id'], 'room_id' => $room_id], ['id' => $deviceInfo['id']]);
            if ($res || $res === 0) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


    }


    public function unBindDevice()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        $devicename = trim(input("request.devicename"));//用户auth
        if (empty($devicename)) {
            $return = ['err' => 1, 'msg' => '设备标识不能为空'];
            return json($return);
        }
        $model = new \app\admin\model\NewwindDevice();
        $deviceInfo = $model->getDataForWhere(['devicename' => $devicename]);
        if (empty($deviceInfo)) {
            $return = ['err' => 1, 'msg' => '设备不存在'];
            return json($return);
        }

        if (empty($deviceInfo['uid'])) {
            $return = ['err' => 1, 'msg' => '设备未被绑定'];
            return json($return);
        }

        if ($deviceInfo['uid'] != $userInfo['id']) {
            $return = ['err' => 100, 'msg' => '你不能解绑其他用户的设备'];
            return json($return);
        }
        $res = $model->updateDataForWhere(['uid' => "0", 'room_id' => "0"], ['id' => $deviceInfo['id']]);
        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    /**
     * 向设备发送消息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sendMsgToDevice()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        $devicename = trim(input("request.devicename"));//用户auth
        if (empty($devicename)) {
            $return = ['err' => 1, 'msg' => '设备标识不能为空'];
            return json($return);
        }

        $msg = trim(input("request.msg"));//用户auth
        if (empty($msg)) {
            $return = ['err' => 1, 'msg' => '消息内容不能为空'];
            return json($return);
        }

        $model = new \app\admin\model\NewwindDevice();
        $deviceInfo = $model->getDataForWhere(['devicename' => $devicename]);
        if (empty($deviceInfo)) {
            $return = ['err' => 1, 'msg' => '设备不存在'];
            return json($return);
        }

        if (empty($deviceInfo['uid'])) {
            $return = ['err' => 1, 'msg' => '设备未被绑定'];
            return json($return);
        }

        if ($deviceInfo['uid'] != $userInfo['id']) {
            $return = ['err' => 100, 'msg' => '你不能给该设备消息'];
            return json($return);
        }


        $url = "http://8.131.73.193:90/demo/device_set_property.php";
        $param = array(
            'iot_id' => $deviceInfo['iot_id'],
            'property_value'=>$msg,
        );
        $getRes = get_url($url, $param);
        if($getRes){
            $temp=json_decode($getRes,true);
            if($temp['err']==0){
                $return = ['err' => 0, 'msg' => '操作成功','other_data'=>$temp['msg']];
                return json($return);
            }else
            {
                $return = ['err' => 1, 'msg' => '操作失败','other_data'=>$temp['msg']];
                return json($return);
            }
        }
       else{
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    public function getDeviceData()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }

        $devicename = trim(input("request.devicename"));//用户auth
        if (empty($devicename)) {
            $return = ['err' => 1, 'msg' => '设备标识不能为空'];
            return json($return);
        }


        $model = new \app\admin\model\NewwindDevice();
        $deviceInfo = $model->getDataForWhere(['devicename' => $devicename]);
        if (empty($deviceInfo)) {
            $return = ['err' => 1, 'msg' => '设备不存在'];
            return json($return);
        }

        if (empty($deviceInfo['uid'])) {
            $return = ['err' => 1, 'msg' => '设备未被绑定'];
            return json($return);
        }

        if ($deviceInfo['uid'] != $userInfo['id']) {
            $return = ['err' => 100, 'msg' => '你不能获取该设备相关数据'];
            return json($return);
        }


        $url = "http://8.131.73.193:90/demo/device_get_single_property.php";
        $param = array(
            'id' => $deviceInfo['id'],
        );

        $getRes = get_url($url, $param);

        if($getRes){
            $temp=json_decode($getRes,true);
            if($temp['err']==0){
                $model = new \app\admin\model\NewwindDevice();
                $deviceInfo = $model->getDataForWhere(['devicename' => $devicename]);
                $return = ['err' => 0, 'msg' => '操作成功','other_data'=>$temp,'data'=>$deviceInfo['device_data']];
                return json($return);
            }else
            {
                $return = ['err' => 1, 'msg' => '操作失败','other_data'=>$temp,'data'=>$deviceInfo['device_data']];
                return json($return);
            }
        }
        else{
            $return = ['err' => 3, 'msg' => '操作失败','data'=>$deviceInfo['device_data']];
            return json($return);
        }

    }
}
